﻿function showRanklist() {
	if (refreshTab('showRanklist')) return;
	var pageSize = 25;

	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : url.getRanklist
						}),
				reader : new Ext.data.JsonReader({
							root : 'result',
							totalProperty : 'total',
							id : 'username'
						}, ['rank', 'nickname', 'username', 'motto', 'problemAccept', 'accept',
								'submit'])
			});
	store.load({
				params : {
					start : 0,
					limit : pageSize
				}
			});

	var cm = new Ext.grid.ColumnModel([{
		header : 'Rank',
		sortable : false,
		dataIndex : 'rank',
		width : 10,
		renderer : function(value, p, record) {
			return String
					.format(
							'<center><a href="javascript:void(0);" onclick="showUserStatistics(\'{1}\');" title="点击查看统计" class="likelink"style="color:{2};font-weight:bold">{0}</a></center>',
							value, record.data.username, value > 3 ? 'black' : 'red');
		}
	}, {
		header : 'Author',
		sortable : false,
		dataIndex : 'username',
		renderer : sys.rd.author,
		width : 22
	}, {
		header : 'Motto',
		sortable : false,
		dataIndex : 'motto'
	}, {
		header : 'ProblemAC',
		sortable : false,
		dataIndex : 'problemAccept',
		width : 15
	}, {
		header : 'Submitions',
		sortable : false,
		dataIndex : 'submit',
		width : 15,
		renderer : function(value, p, record) {
			return record.data.accept + ' / ' + value;
		}
	}, {
		header : 'Ratio',
		sortable : false,
		dataIndex : 'submit',
		width : 11,
		renderer : function(value, p, record) {
			return sys.rd.ratio(record.data.accept, value);
		}
	}]);

	var pagebar = new Ext.PagingToolbar({
				pageSize : pageSize,
				store : store,
				displayInfo : true,
				emptyMsg : '当前没有用户。',
				displayMsg : '当前显示前 {0} - {1} 名，共 {2} 位用户。',
				items : [new Ext.Toolbar.Separator(), new Ext.Button({
									text : '转到我',
									iconCls : 'goto-icon',
									handler : onme
								})],
				plugins : new Ext.ux.SlidingPager()
			});

	var grid = new Ext.grid.GridPanel({
				title : 'RankList',
				id : 'showRanklist',
				iconCls : 'ranklist-icon',
				closable : true,
				region : 'center',
				loadMask : {
					msg : '载入中...'
				},
				viewConfig : {
					forceFit : true,
					scrollToRow : function(rowIndex) {
						var firstRow = Ext.get(this.getRow(0));
						var row = Ext.get(this.getRow(rowIndex));
						var distance = row.getOffsetsTo(firstRow)[1];
						this.scroller.dom.scrollTop = distance;
					}
				},
				store : store,
				cm : cm,
				bbar : pagebar
			});
	grid.on('rowdblclick', ondblclick);
	grid.onRefresh = function() {
		store.reload();
	}
	addTab(grid);

	function onme() {
		if (sys.guest) return;
		getUserRank({
					username : sys.user.username,
					success : function(obj) {
						store.load({
									params : {
										start : obj.point - obj.point % pageSize,
										limit : pageSize
									},
									callback : function() {
										grid.getSelectionModel().selectRow(obj.point % pageSize);
										new Ext.util.DelayedTask(function() {
													grid.getView()
															.scrollToRow(obj.point % pageSize);
												}).delay(300);
									}
								});
					}
				});
	}
	function getSelect() {
		if (grid.getSelectionModel().getCount() == 0) return null;
		return grid.getSelectionModel().getSelected();
	}
	function ondblclick() {
		if (sys.guest) return;
		var record = getSelect();
		if (!record) return;
		showUserStatistics(record.data.username);
	}
}

function getUserRank(obj) {
	ajax({
				url : url.getUserRank,
				params : {
					username : obj.username
				},
				success : obj.success
			});
}
